package com.my.yc.d0301;

import com.google.gson.Gson;
import com.my.yc.bean.Result;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AddCartServlet", value = "/AddCartServlet")
public class AddCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String name = request.getParameter("name");
        HttpSession session = request.getSession();
        //获取原有数量
        Integer nums = (Integer) session.getAttribute(name);
        if(nums == null){
            nums = 0;
        }
        nums++;
        session.setAttribute(name, nums);
        Result res = new Result(1,"添加成功", null);
        String json = new Gson().toJson(res);
        response.getWriter().append(json);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
